package guoanmaker.operator.business.service;

import guoanmaker.operator.business.model.OperatorFriendshipLink;
import guoanmaker.operator.business.repository.OperatorFriendshipLinkRepository;
import guoanmaker.operator.helper.common.AjaxJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;

/**
 *  
 *
 * @ClassName: OperatorFriendshipLinkService 
 * @Description: OperatorFriendshipLinkService 
 * @author: huZhaoyu 
 * @date 2017/6/28 下午2:06 
 */
@Service
public class OperatorFriendshipLinkService implements OperatorFriendshipLinkServiceInterface {
    private final OperatorFriendshipLinkRepository operatorFriendshipLinkRepository;
    @PersistenceContext
	private EntityManager manager;

    @Autowired
    public OperatorFriendshipLinkService(OperatorFriendshipLinkRepository operatorFriendshipLinkRepository) {
        this.operatorFriendshipLinkRepository = operatorFriendshipLinkRepository;
    }

    @Override
    @Transactional
    public OperatorFriendshipLink saveFriendshipLink(OperatorFriendshipLink operatorFriendshipLink) {
        return operatorFriendshipLinkRepository.save(operatorFriendshipLink);
    }

    @Override
    public List<OperatorFriendshipLink> getAllFriendshipLink(String type) {
        return operatorFriendshipLinkRepository.findByType(type);
    }

    @Override
    public Page<OperatorFriendshipLink> getAllFriendshipLinkByPage(Integer page, Integer size) {
        return operatorFriendshipLinkRepository.findAll(new PageRequest(page, size));
    }

    @Override
    @Transactional
    public void deleteFriendshipLinkById(String id) {
        operatorFriendshipLinkRepository.delete(id);
    }

    @Override
    public OperatorFriendshipLink getFriendshipLinkById(String id) {
        return operatorFriendshipLinkRepository.findOne(id);
    }

    @Override
    public Page<OperatorFriendshipLink> getAllFriendshipLinkByWeightAndPage(Integer page, Integer size) {
        return operatorFriendshipLinkRepository.findAll(new PageRequest(page, size, new Sort(Sort.Direction.DESC, "weight")));
    }

	@Override
	public List<OperatorFriendshipLink> getAllFriendshipLinkByCityCode(String cityCode) {
		// TODO Auto-generated method stub
		return operatorFriendshipLinkRepository.findByCityCode(cityCode);
	}
	 @Transactional
	  public void delectAllBycitycode(String type){
		  String jpql="delete  from OperatorFriendshipLink  u where u.type=:type";
		  Query query2=manager.createQuery(jpql);//查询商品提供的服务区域
			query2.setParameter("type", type);
			query2.executeUpdate();
		  
	  }
}
